<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            width: 300px;
            position: absolute;
        }
    </style>
</head>

<body>
    <img src="http://contentcms-bj.cdn.bcebos.com/cmspic/a3e3de575e8807c1063fc6d476abee1e.jpeg">
</body>

</html>
<script>
    //限定范围的拖拽
    //固定的盒子内拖拽元素。
    //onmousemove时候，限定盒子的left，top最小不能为负数。最大值不能超过(可视区-盒子的尺寸).
    var oImg = document.querySelector('img');
    oImg.onmousedown = function(ev) {
        var ev = ev || event;
        var shortlinex = ev.offsetX;
        var shortliney = ev.offsetY;
        document.onmousemove = function(ev) {
            var ev = ev || event;
            //限定范围
            var left = ev.clientX - shortlinex; //盒子的left位置
            var top = ev.clientY - shortliney; //盒子的left位置
            if (left < 0) {
                left = 0;
            } else if (left > document.documentElement.clientWidth - oImg.offsetWidth) {
                left = document.documentElement.clientWidth - oImg.offsetWidth;
            }

            if (top < 0) {
                top = 0;
            } else if (top > document.documentElement.clientHeight - oImg.offsetHeight) {
                top = document.documentElement.clientHeight - oImg.offsetHeight;
            }
            oImg.style.left = left + 'px';
            oImg.style.top = top + 'px';
        };
        document.onmouseup = function() {
            document.onmousemove = null;
            document.onmouseup = null;
        };

        return false; //取消图片的默认行为。
    }
</script>